package indexer;
import java.util.*;

public class TermDocument {
	private Vector<Integer> pos;
	private Integer num;
	private Boolean sorted;
	
	public TermDocument (){
		pos=new Vector<Integer>();
		num=new Integer(0);
		sorted=new Boolean(true);
	}
	public void add (int addpos){
		if (num>0 && pos.lastElement()>addpos)
			sorted=false;
		if (exist(addpos)){
			System.out.println("Warning Message from TermDocument: "
					+ "the pos to add already exists");
			return;
		}
				
		pos.add(addpos);
		num++;
	}
	public void add (Vector<Integer> posvec){
		Enumeration<Integer> en=posvec.elements();
		while (en.hasMoreElements()){
			add(en.nextElement());
		}
	}
	public boolean exist (int existpos){
		return pos.contains(existpos);
	}
	public void delete (int delpos){
		if (!exist(delpos)){
			System.out.println("Warning Message from TermDocument: "
					+ "the pos to delete does not exist");
			return;
		}
		pos.removeElement(delpos);
		num--;
	}
	public void sort (){
		if (sorted)
			return;
		Integer [] numbers=new Integer[pos.size()];
		Enumeration<Integer> en =pos.elements();
		int count=0;
		do{
			numbers[count++]=en.nextElement();
		}while(en.hasMoreElements());
		numbers=QuickSort.Sort(numbers);
		pos.removeAllElements();
		for (int i=0;i<num;i++)
			pos.add(numbers[i]);
		sorted=true;
	}
	public Vector<Integer> returnpos(){
		Vector<Integer> ppos=new Vector<Integer>(pos.size());
		Enumeration<Integer> en=pos.elements();
		int temp;
		do{
			temp=en.nextElement();
			ppos.add(temp);
		}while(en.hasMoreElements());
		return ppos;
	}
	public boolean issorted(){
		return sorted;
	}
}
